import 'package:casdon/core/logger/log.dart';
import 'package:casdon/core/network/business_exception.dart';
import 'package:casdon/modules/recipe/data/api/miffy_recipe_cloud_list_api.dart';

import 'package:casdon/modules/recipe/data/repository/recipe_repository.dart';
import 'package:casdon/modules/recipe/providers/recipe_provider.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

class MiffyRecipeCloudListUsecase {
  final RecipeRepository _repository;

  const MiffyRecipeCloudListUsecase(this._repository);

  Future<ResponseMiffyRecipeCloudListApi> execute({
    required String pid,
    required int pageNum,
    required int pageSize,
  }) async {
    try {
      final request = RequestMiffyRecipeCloudListApi(
        pageNo: pageNum,
        pageSize: "20",
        productIds: [pid],
      );
      var ret = await _repository.getMiffyRecipeCloudList(request);
      ret ??= ResponseMiffyRecipeCloudListApi(0, []);
      return ret;
    } on BusinessException {
      rethrow;
    } catch (e) {
      Log.e("MiffyRecipeCloudListUsecase 失败", e.toString(), StackTrace.current);
      throw Exception("MiffyRecipeCloudListUsecase 失败");
    }
  }
}

final miffyRecipeCloudListUsecaseProvider = Provider(
  (ref) => MiffyRecipeCloudListUsecase(ref.read(recipeRepository)),
);
